package com.kekezu.kppw.data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.tsz.afinal.FinalDb;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.widget.Toast;

import com.kekezu.kppw.bean.UserBean;
import com.kekezu.kppw.utils.ConfigInc;
import com.kekezu.kppw.utils.HttpUtils;

public class OrderData {
	static FinalDb db;

	/**
	 * 雇主购买的服务或作品
	 * 
	 * @param type
	 * @param goodStatus
	 * @param serviceStatus
	 * @param page
	 * @param context
	 * @return
	 */
	public static ArrayList<HashMap<String, Object>> getGoodOrder(String type,
			String goodStatus, String serviceStatus, int page, Context context) {
		ArrayList<HashMap<String, Object>> taskList = new ArrayList<HashMap<String, Object>>();
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/myBuyGoods?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		Map<String, String> params = new HashMap<String, String>();
		params.put("type", type);
		params.put("goods_status", goodStatus);
		params.put("service_status", serviceStatus);
		params.put("token", users.get(0).getToken());
		params.put("page", String.valueOf(page));

		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);
			String resCode = jsonObject.getString("code");
			String resMsg = jsonObject.getString("code");

			// 对返回的数据进行判断
			if (resCode.equals("1000")) {
				JSONObject jsondata = new JSONObject(
						jsonObject.getString("data"));
				JSONArray jsonArray = jsondata.getJSONArray("data");

				for (int i = 0; i < jsonArray.length(); i++) {
					HashMap<String, Object> map = new HashMap<String, Object>();
					JSONObject itemJsonObject = jsonArray.optJSONObject(i);

					if (type.equals("1")) {
						map.put("id", itemJsonObject.getString("id"));
						map.put("title", itemJsonObject.getString("title"));
						map.put("cash", itemJsonObject.getString("cash"));
						map.put("status", itemJsonObject.getString("status"));
						map.put("created_at",
								itemJsonObject.getString("created_at"));
						map.put("cate_id", itemJsonObject.getString("cate_id"));
						map.put("unit", "元/" + itemJsonObject.getString("unit"));
						map.put("name", itemJsonObject.getString("name"));
						map.put("cover", itemJsonObject.getString("img"));
						map.put("cate_name",
								itemJsonObject.getString("cate_name"));
						map.put("sales_num",
								itemJsonObject.getString("sales_num"));
						map.put("comments_num",
								itemJsonObject.getString("comments_num"));
						map.put("good_comment",
								itemJsonObject.getString("good_comment"));
						map.put("status_num",
								itemJsonObject.getString("status_num"));
						map.put("percent", itemJsonObject.getString("percent"));
					} else {
						map.put("id", itemJsonObject.getString("id"));
						map.put("title", itemJsonObject.getString("title"));
						map.put("status", itemJsonObject.getString("status"));
						map.put("employee_uid",
								itemJsonObject.getString("employee_uid"));
						map.put("employ_type",
								itemJsonObject.getString("employ_type"));
						map.put("created_at",
								itemJsonObject.getString("created_at"));
						map.put("employee_num",
								itemJsonObject.getString("employee_num"));
						map.put("status_num",
								itemJsonObject.getString("status_num"));
						map.put("cash", itemJsonObject.getString("cash"));
						map.put("unit", "元");
						map.put("cover", itemJsonObject.getString("img"));

						map.put("sales_num",
								itemJsonObject.getString("comments_num"));
						// map.put("sales_num",
						// itemJsonObject.getString("good_comment"));
						map.put("percent", itemJsonObject.getString("percent"));
					}

					taskList.add(map);
				}
			} else {
				Toast.makeText(context, resMsg, Toast.LENGTH_SHORT).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return taskList;
	}

	/**
	 * 卖出的服务订单
	 * 
	 * @param type
	 * @param goodStatus
	 * @param serviceStatus
	 * @param page
	 * @param context
	 * @return
	 */
	public static ArrayList<HashMap<String, Object>> getSaleOrder(String type,
			String goodStatus, String serviceStatus, int page, Context context) {
		ArrayList<HashMap<String, Object>> taskList = new ArrayList<HashMap<String, Object>>();
		String urlString = ConfigInc.getServiceAdress(context)
				+ "user/mySaleGoods?";
		HttpUtils httpUtils = HttpUtils.getInstance();

		db = ConfigInc.getCreateDB(context);
		List<UserBean> users = db.findAll(UserBean.class);

		Map<String, String> params = new HashMap<String, String>();

		params.put("type", type);
		params.put("goods_status", goodStatus);
		params.put("service_status", serviceStatus);
		params.put("token", users.get(0).getToken());
		params.put("page", String.valueOf(page));

		try {
			String strJson = httpUtils.httpGet(urlString, params, context);
			JSONObject jsonObject = new JSONObject(strJson);
			String resCode = jsonObject.getString("code");
			// String resMsg = jsonObject.getString("message");

			// 对返回的数据进行判断
			if (resCode.equals("1000")) {
				JSONObject jsondata = new JSONObject(
						jsonObject.getString("data"));
				JSONArray jsonArray = jsondata.getJSONArray("data");

				for (int i = 0; i < jsonArray.length(); i++) {
					HashMap<String, Object> map = new HashMap<String, Object>();
					JSONObject itemJsonObject = jsonArray.optJSONObject(i);

					if (type.equals("1")) {
						map.put("id", itemJsonObject.getString("id"));
						map.put("title", itemJsonObject.getString("title"));
						map.put("cash", itemJsonObject.getString("cash"));
						map.put("status", itemJsonObject.getString("status"));
						map.put("created_at",
								itemJsonObject.getString("created_at"));
						map.put("cate_id", itemJsonObject.getString("cate_id"));
						map.put("unit", itemJsonObject.getString("unit"));
						map.put("name", itemJsonObject.getString("name"));
						map.put("cover", itemJsonObject.getString("avatar"));
						map.put("cate_name",
								itemJsonObject.getString("cate_name"));
						map.put("sales_num",
								itemJsonObject.getString("sales_num"));
						map.put("comments_num",
								itemJsonObject.getString("comments_num"));
						map.put("good_comment",
								itemJsonObject.getString("good_comment"));
						map.put("status_num",
								itemJsonObject.getString("status_num"));
						map.put("percent", itemJsonObject.getString("percent"));
					} else {
						map.put("id", itemJsonObject.getString("id"));
						map.put("title", itemJsonObject.getString("title"));
						map.put("status", itemJsonObject.getString("status"));
						map.put("employer_uid",
								itemJsonObject.getString("employer_uid"));
						map.put("employ_type",
								itemJsonObject.getString("employ_type"));
						map.put("created_at",
								itemJsonObject.getString("created_at"));
						map.put("user_name",
								itemJsonObject.getString("user_name"));
						map.put("cash", itemJsonObject.getString("cash"));
						map.put("employee_num",
								itemJsonObject.getString("employee_num"));
						map.put("status_num",
								itemJsonObject.getString("status_num"));
						map.put("cover", itemJsonObject.getString("avatar"));
						map.put("comments_num",
								itemJsonObject.getString("comments_num"));
						map.put("good_comment",
								itemJsonObject.getString("good_comment"));
						map.put("percent", itemJsonObject.getString("percent"));
						map.put("unit", " ");
						map.put("sales_num", " ");
					}

					taskList.add(map);
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return taskList;
	}
}
